import {getCount} from '@/api/mall/goodsspecvalue'
const isNumber = (rule, value, callback) => {
    if (value === '') {
      return callback(new Error('请填写数量'));
    }  //这是用来判断如果不是必须输入的，则直接跳出
    const r = /^([1-9]\d*|[0]{1,1})$/; //含0正整数
    // 如果判断不符合正则，则不是正整数不能提交
    if (!r.test(value)) {
      return callback(new Error('数量必须为正整数'));
    } else {
      return callback();
    }
}

const validateName = (rule, value, callback) => {
  if (window.openType === 'edit'){
    callback()
  }else{
    getCount({
      name: value
    }).then(response => {
      let data = response.data.data
      if (data > 0) {
        callback(new Error('名称已经存在'))
      } else {
        callback()
      }
    })
  }
}


export const tableOption = {
    dialogDrag: true,
    border: true,
    stripe: true,
    menuAlign: 'center',
    align: 'center',
    menuType: 'text',
    searchShow: false,
    excelBtn: true,
    printBtn: true,
    viewBtn: true,
    searchMenuSpan: 6,
    column: [
        {
            label: '名称',
            prop: 'name',
            sortable: true,
            rules: [{
              required: true,
              message: '请输入名称',
              trigger: 'blur'
            },{
              max: 50,
              message: '长度在不能超过50个字符'
            },{
              validator: validateName,
              trigger: 'blur'
            }]
        },
      {
        label: '类型',
        prop: 'type',
        type: 'select',
        rules: [{
          required: true,
          message: '请选择类型',
          trigger: 'blur'
        }],
        dicData: [{
          label: '次(课)数',
          value: 0
        }, {
          label: '月数',
          value: 1
        }
          , {
            label: '无',
            value: 2
          }]
      },
      {
        label: '数量',
        prop: 'frequency',
        type: 'number',
        sortable: true,
        rules: [{
          validator: isNumber,
          required: true,
          trigger: 'blur'
        }]
      },
        {
            label: '创建时间',
            prop: 'createTime',
            sortable: true,
            addDisplay: false,
            editDisplay: false
        },
        {
            label: '更新时间',
            prop: 'updateTime',
            sortable: true,
            addDisplay: false,
            editDisplay: false
        },
    ]
}
